

window.onload=function () {
    findAll();
}

function findAll() {
    axios.get("/user/findAll").then(resp=>{
        var info = resp.data;
        var str = "";
        info.forEach(n=>{
            str+=`<tr><td>${n.userName}</td><td>${n.password}</td><td>${n.name}</td>
<td>${n.grade}</td><td><input type="button" value="删除用户" style="margin-left:20px;height: 30px" onclick="del(${n.id})">
<input type="button" value="修改用户等级" style="margin-left:20px;height: 30px" onclick="updateFind(${n.id})"></td></tr>`;
        })
        $n("userData").innerHTML=str;
    });
}

function del(id) {
    axios.get("/user/del/",{
        params:{
            id
        }
    }).then(resp=>{
        var info = resp.data;
        if (info == "ok"){
            findAll();
        }else {
            $n("infoDiv").style.visibility="visible";
        }
    });
}
var emId;
function updateFind(id) {
    emId=id;
    axios.get("/user/findById",{
        params: {
            id
        }
    }).then(resp=>{
        var info = resp.data;
        $n("updateUserName").innerHTML=info.userName;
        $n("updateName").innerHTML=info.name;
    })
    $n("updateDiv").style.visibility="visible";
}

function update() {
    axios.get("/user/updateGrade",{
        params:{
            id:emId,
            grade:$n("updateGrade").value
        }
    }).then(resp=>{
        var info = resp.data;
        if (info=="ok"){
            $n("updateDiv").style.visibility="hidden";
            findAll();
        }
    })
}
function add() {
    axios.get("/user/add",{
        params:{
            userName:$n("addUserName").value,
            name:$n("addName").value,
            grade: $n("addGrade").value
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            $n("addDiv").style.visibility="hidden";
            findAll();
            $n("addUserName").value="";
            $n("addName").value="";
        }else {
            $n("addInfoDiv").style.visibility="visible";
        }
    })
}
function updateStu() {
    $n("updateDiv").style.visibility="hidden";
}
function addDiv() {
    $n("addDiv").style.visibility="visible"
}
function addStu() {
    $n("addUserName").value="";
    $n("addName").value="";
    $n("addDiv").style.visibility="hidden"
}
function delInfo() {
    $n("infoDiv").style.visibility="hidden"
}
function addInfo() {
    $n("addInfoDiv").style.visibility="hidden"
}